package com.Gameplay.character.Role;

import com.Gameplay.Combat.Attack.AttackType;
import com.Gameplay.Combat.CombatManagement.CombatManagement;
import com.Gameplay.Combat.Hitbox.impl.RectangleHitbox;
import com.Gameplay.Combat.Hurtbox.FistHurtbox;
import com.Gameplay.Combat.Hurtbox.LegsHurtbox;
import com.Gameplay.character.Action.Moving1;
import com.Gameplay.character.Role.Behavior.RoleBehavior;
import com.Input.InputManager;
import com.core.Window.Main.Game;
import javafx.scene.input.KeyCode;

import java.io.Serializable;
import java.util.ArrayList;

import static java.lang.Math.abs;

//角色父类
public class parentRole implements RoleBehavior, Serializable {
    private final int id;
    private final int clientId;
    private final String name;

    //获取屏幕信息;
    private final int ComputerHeight = Game.getInstance().getHeight();
    private final int ComputerWidth = Game.getInstance().getWidth();
    //当前角色位置信息
    private double currentX = (double) ComputerWidth / 2 + (double) ComputerWidth / 4;
    private double currentY = (double) ComputerHeight / 2 + (double) ComputerHeight / 7;
    //先前角色位置信息
    private double preX = currentX;
    private double preY = currentY;
    private double pushCurrentX;
    //角色移动速度
    private final double speedX = 10;
    private final double speedY = -2000;
    //受击框绘制
    private RectangleHitbox roleBox;
    //攻击框绘制
    private FistHurtbox fistBox;
    private LegsHurtbox legsBox;
    private double FistLength;
    private double LegsLength;
    //先前角色状态（血量，精力）
    private double Health;
    private double Energy;
    private double Damage;
    private int SkillSlot;

    //角色攻击类型
    private AttackType attackType;
    //角色攻击状态
    private boolean isFistAttacking = false;
    private boolean isLegsAttacking = false;
    private boolean canAttacking = true;
    //角色防御状态
    private boolean isblocking = false;
    //检测角色是否处于移动状态
    private boolean isMoving = false;
    //角色朝向
    private int facing = 0;
    //角色战斗系统初始化
    private final CombatManagement management = new CombatManagement();
    //角色信息获取
    private double damage;
    //防御扣除的精力
    private double energy;
    private ArrayList<Double> information;
    private Moving1 moving1;
    public parentRole(int id, int clientId, String name) {

        this.id = id;
        this.clientId = clientId;
        this.name = name;
        Health = 100;
        roleBox = new RectangleHitbox();
        fistBox = new FistHurtbox();
        legsBox = new LegsHurtbox();
        information = new ArrayList<>();
        moving1 = new Moving1(this);
    }

    public void update(double x, double y, double pushX) {

        if (preX != currentX) {
            preX = currentX;
        }
        if (preY != currentY) {
            preY = currentY;
        }

        currentX = x;
        currentY = y;
        if (pushX != 0) {
            currentX = pushX;
        }
        roleBox.setBox(currentX, currentY);
    }

    public void updateHealth(double Damage) {
        setHealth(Health - Damage);
    }


    //角色朝向，0是左，1是右
    public void facing(parentRole entity) {

        if (currentX >= entity.currentX) {
            //面向左
            facing = 0;
        } else {
            //面向右
            facing = 1;
        }
    }

    @Override
    public void Idle() {

    }

    @Override
    public void Moving(parentRole entity) {
        moving1.Moving(entity);
    }


    //边界检测
    public void boundaryDetection(parentRole entity) {
        double instance = abs(currentX - entity.getCurrentX());
        //左边界检测
        if (instance >= 1290) {
            currentX = preX;
        }
        if (roleBox.getLwidth() <= 0) {
            currentX = 75;
        }
        //p2的左边框小于左边界并且p1的左边框小于p2的右边框并且p1和p2在地面上
        if (entity.roleBox.getLwidth() <= 0 && roleBox.getLwidth() <= entity.roleBox.getRwidth() && entity.currentY >= (double) ComputerHeight / 2 + (double) ComputerHeight / 7 && currentY >= (double) ComputerHeight / 2 + (double) ComputerHeight / 7) {
            currentX = 225;
        }
        //右边界检测
        if (instance >= 1290) {
            currentX = preX;
        }
        if (roleBox.getRwidth() >= ComputerWidth + 1440.0D) {
            currentX = 2805;
        }
        //p2的右边框大于右边界并且p1的右边框大于p2的左边框并且p1和p2在地面上
        if (entity.roleBox.getRwidth() >= ComputerWidth + 1440.0D && roleBox.getRwidth() >= entity.roleBox.getLwidth() && entity.currentY >= (double) ComputerHeight / 2 + (double) ComputerHeight / 7 && currentY >= (double) ComputerHeight / 2 + (double) ComputerHeight / 7) {
            currentX = 2655;
        }
    }




    @Override
    public void Attacking(ArrayList<KeyCode> keys, parentRole entity) {
        isFistAttacking = false;
        isLegsAttacking = false;
        Damage = 0;
        //释放的按键
        for (KeyCode keyCode : InputManager.getPressed()) {
            if ((keyCode == KeyCode.I | keyCode == KeyCode.U | keyCode == KeyCode.O) && canAttacking) {
                Damage = management.FistCombat(keyCode, this, entity).getFirst();
                isFistAttacking = true;
                canAttacking = false;
                break;
            }
            if ((keyCode == KeyCode.J | keyCode == KeyCode.K | keyCode == KeyCode.L) && canAttacking) {
                management.LegsCombat(keyCode, this, entity);
                isLegsAttacking = true;
                canAttacking = false;
                break;
            }
        }
        if (keys != null && !keys.isEmpty()) {
            if (keys.getFirst() == KeyCode.U | keys.getFirst() == KeyCode.I | keys.getFirst() == KeyCode.O) {
                canAttacking = true;

            } else if (keys.getFirst() == KeyCode.J || keys.getFirst() == KeyCode.K || keys.getFirst() == KeyCode.L) {
                canAttacking = true;
            }
        }

    }

    @Override
    public void Blocking() {
    }

    @Override
    public void HitSun() {
    }

    @Override
    public void etc() {
    }

    public void Moving1update(){
        if (preX != currentX) {
            preX = currentX;
        }
        if (preY != currentY) {
            preY = currentY;
        }
    }

    public boolean isFistAttacking() {
        return isFistAttacking;
    }

    public boolean isLegsAttacking() {
        return isLegsAttacking;
    }

    public boolean isIsblocking() {
        return isblocking;
    }

    public void setIsblocking(boolean isblocking) {
        this.isblocking = isblocking;
    }

    public void setEnergy(double energy) {
        Energy = energy;
    }

    public void setHealth(double health) {
        Health = health;
    }

    public double getEnergy() {
        return Energy;
    }

    public double getHealth() {
        return Health;
    }


    public RectangleHitbox getRoleBox() {
        return roleBox;
    }

    public FistHurtbox getFistBox() {
        return fistBox;
    }

    public LegsHurtbox getLegsBox() {
        return legsBox;
    }

    public boolean canAttacking() {
        return canAttacking;
    }

    public int getFacing() {
        return facing;
    }

    public double getPushCurrentX() {
        return moving1.getPushCurrentX();
    }

    public double getDamage() {
        return Damage;
    }

    public int getSkillSlot() {
        return SkillSlot;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void GamesetCurrentX(double currentX){
        preX = currentX;
        this.currentX = currentX;
    }

    public void setCurrentX(double currentX) {
        this.currentX = currentX;
    }

    public void setCurrentY(double currentY) {
        this.currentY = currentY;
    }
    public void setisMoving(boolean moving){
        this.isMoving = moving;
    }

    public double getSpeedY() {
        return speedY;
    }

    public double getSpeedX() {
        return speedX;
    }

    public double getPreY() {
        return preY;
    }

    public double getPreX() {
        return preX;
    }

    public double getCurrentY() {
        return currentY;
    }

    public double getCurrentX() {
        return currentX;
    }

    public boolean getisMoving() {
        return isMoving;
    }

    public int getClientId() {
        return clientId;
    }
}

